<script  setup lang="ts">
import { ref, watch } from 'vue';
import { resetPassword, resetPayPass, loginOut, getUserInfo, } from '/@/apis/userApi';
import { useRequest } from 'vue-request';
import { isNull, NotifyWarn, pwdReg, NotifySuccess } from '/@/utils/';
import { i18nTranslate } from '/@/i18n';
import { useRouter } from 'vue-router';
import { useToken, useUserId } from '/@/hooks';
import { Md5 } from 'ts-md5';
const router = useRouter();
const { getUserId, setUserId } = useUserId();
const { setToken } = useToken();
const tabActive = ref(0);
const oldPassword = ref();
const newPassword = ref();
const confirmPassword = ref();
const isPay = ref(true)
const list = ref([{
  title: 'Oldpassword',
  maxlength: 6,
  name: '',
  showsz: false,
  },
  {
    title: 'Newpassword',
    maxlength: 6,
    name: '',
    showsz: false,
  },
  {
    title: 'Confirmpassword',
    maxlength: 6,
    name: '',
    showsz: false,
  },
])
useRequest(getUserInfo, {
  defaultParams: [{member: getUserId()}],
  onSuccess(data: any) {
    isPay.value = data.bankCard !== '' ? true : false;
  },
})
const handleFocus = (item: any) => {
  list.value.forEach((val) => {
      val.showsz = false;
  });
  item.showsz = true;
}
const Cinput = (key: any) => {
  // console.log(key);
}
const onDelete = (name: any, key: any) => {
  // console.log(name, key);
}
const requestLoginOut = useRequest(loginOut, {
  manual: true,
  onSuccess(data: any) {
    setUserId('');
    setToken('');
    router.push('/login');
  },
});
const requestResetPassword = useRequest(resetPassword, {
  manual: true,
  onSuccess(data: any) {
    NotifySuccess(i18nTranslate('ResetSuccessful'));
    requestLoginOut.run({ member: getUserId() });
  },
  onError(data: any) {
    return;
  },
});
const requestResetPayPass = useRequest(resetPayPass, {
  manual: true,
  onSuccess(data: any) {
    NotifySuccess(i18nTranslate('ResetSuccessful'));
    router.go(-1);
  },
  onError(data: any) {
    return;
  },
});
const onSubmit = async () => {
  if (tabActive.value === 0) {
    if (isNull(oldPassword.value)) {
      NotifyWarn(i18nTranslate('Oldpassword'));
    } else if (!pwdReg(oldPassword.value)) {
      NotifyWarn(i18nTranslate('passwordNum'));
    } else if (isNull(newPassword.value)) {
      NotifyWarn(i18nTranslate('Newpassword'));
    } else if (!pwdReg(newPassword.value)) {
      NotifyWarn(i18nTranslate('passwordNum'));
    } else if (isNull(confirmPassword.value)) {
      NotifyWarn(i18nTranslate('Confirmpassword'));
    } else if (!pwdReg(confirmPassword.value)) {
      NotifyWarn(i18nTranslate('passwordNum'));
    } else if (newPassword.value != confirmPassword.value) {
      NotifyWarn(i18nTranslate('samePasswords'));
    } else {
      const params = {
        member: getUserId(),
        oldPass: Md5.hashStr(oldPassword.value),
        newPass: Md5.hashStr(newPassword.value),
      };
      requestResetPassword.run(params);
    }
  } else if (tabActive.value === 1) {
    if (isNull(list.value[0].name)) {
      NotifyWarn(i18nTranslate('Oldpassword'));
    } else if (!pwdReg(list.value[0].name)) {
      NotifyWarn(i18nTranslate('passwordNum'));
    } else if (isNull(list.value[1].name)) {
      NotifyWarn(i18nTranslate('Newpassword'));
    } else if (!pwdReg(list.value[1].name)) {
      NotifyWarn(i18nTranslate('passwordNum'));
    } else if (isNull(list.value[2].name)) {
      NotifyWarn(i18nTranslate('Confirmpassword'));
    } else if (!pwdReg(list.value[2].name)) {
      NotifyWarn(i18nTranslate('passwordNum'));
    } else if (list.value[1].name != list.value[2].name) {
      NotifyWarn(i18nTranslate('samePasswords'));
    } else {
      if (isPay.value) {
        const params2 = {
          member: getUserId(),
          oldPass: list.value[0].name,
          password: list.value[1].name,
        };
        requestResetPayPass.run(params2);
      } else {
        NotifyWarn(i18nTranslate('Pleaseaddbankcardfirst'));
      }
    }
  }
};
// 切换方式，数据清空
watch(tabActive, () => {
  oldPassword.value = '';
  newPassword.value = '';
  confirmPassword.value = '';
  list.value[0].name = '';
  list.value[1].name = '';
  list.value[2].name = '';
});
</script>
<template>
  <div class="password_settings">
    <AppBar back :primary="true" :title="$t('Passwordsettings')"></AppBar>
    <div class="password_settings_form">
      <van-tabs v-model:active="tabActive">
        <!-- 登录密码 -->
        <van-tab :title="$t('Login')">
          <InputPassword :label="$t('Oldpassword')" v-model="oldPassword" :placeholder="$t('PleaseenteryourOldpassword')"></InputPassword>
          <InputPassword :label="$t('Newpassword')" v-model="newPassword"  :placeholder="$t('PleaseenteryourNewpassword')"></InputPassword>
          <InputPassword  :label="$t('Newpassword')" v-model="confirmPassword"  :placeholder="$t('PleaseenteryourConfirmpassword')"></InputPassword>
        </van-tab>
        <!-- 交易 密码 -->
        <van-tab :title="$t('Trade')">
          <div v-for="(item, index) in list" :key="index">
            <div class="trade_lable">{{ $t(item.title) }}</div>
            <van-password-input :value="item.name" :gutter="8" :focused="item.showsz" @focus="handleFocus(item)" />
            <van-number-keyboard v-model="item.name" :maxlength="6" :show="item.showsz" @blur="item.showsz = false" @input="Cinput" @delete="onDelete(item.name, index)" />  
          </div>
        </van-tab>
      </van-tabs>
     <LargePrimaryButton block class="submit_button" @click="onSubmit">{{ $t('Confirm') }}</LargePrimaryButton>
    </div>
  </div>
</template>
<style lang="scss" scoped>
.password_settings {
  .password_settings_form {
    margin-top: 10px;
    padding: 0 50px;
    .obtain {
      font-size: 32px;
      color: #4CA335;
    }
    .icon_box {
      width: 36px;
      height: 36px;
      margin-right: 40px;
      img {
        width: 100%;
        height: 100%;
      }
    }
    .argee_box {
      margin-top: 70px;
      margin-bottom: 24px;
      font-size: 26px;
      color: #161827;
      .privacy {
        color: #2f73ff;
        padding-left: 10px;
      }
    }
   .trade_lable {
     font-size: 32px;
     margin: 25px 0 25px;
     color: rgba(255, 255, 255, 0.85);
   }
   .submit_button {
    margin-top: 30px;
   }
  }
}
</style> 
<style lang="scss">
.password_settings {
  .van-tab__text--ellipsis {
    font-size: 30px;
    font-weight: 500;
    color: #999999;
  }
  .van-tabs__nav {
    height: 60px;
    background-color: $body;
  }
  .van-tab {
    background: #242424;
    border-radius: 10px;
    color: rgba(255, 255, 255, 0.45);
    font-size: 28px;
    margin-right: 32px;
    padding: 0 45px;
    height: 62px;
  }
  .van-tab--active {
    background: rgba(0, 99, 245, 0.1);
    .van-tab__text {
      color: rgb(0, 99, 245);
    }
  }
  .van-tabs__line {
  display: none;
  }
  .hl-input-box .var-input .var-field-decorator__controller {
    height: 98px;
  }
  .van-password-input {
    margin: 0;
  }
  .van-password-input__security li {
        width: 98px;
        height: 98px;
        background: #242424;
        border-radius: 8px;
    }
  .van-number-keyboard {
    background-color: #242424;
  }
  .van-key {
    background-color: #181717;
  }
  .van-password-input__security i {
    background-color: rgba(255, 255, 255, 0.85);
  }
  .van-password-input__cursor {
    background-color: rgba(255, 255, 255, 1);
  }
}
</style>